package com.itheima.a23;

import org.springframework.beans.*;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestParameterPropertyValues;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;

/**
 * @author: AirMan
 * @date: 2025/4/18 13:59
 * @description:
 */
public class MyTestTypeConverter {
    public static void main(String[] args) {

        // 1.SimpleTypeConverter ==> 仅做类型转换
        System.out.println("-----------------------SimpleTypeConverter-----------------------");
        SimpleTypeConverter simpleTypeConverter = new SimpleTypeConverter();
        Integer number = simpleTypeConverter.convertIfNecessary("13", int.class);
        Date date = simpleTypeConverter.convertIfNecessary("1999/09/18", Date.class);
        System.out.println("number = " + number);
        System.out.println("date = " + date);

        // 2.BeanWrapperImpl ==> 为Bean的属性值赋值，必要时做类型转转换（走Property）
        System.out.println("-----------------------BeanWrapperImpl-----------------------");
        TestBeanWrapperImpl bean = new TestBeanWrapperImpl(); // bean
        BeanWrapperImpl beanWrapper = new BeanWrapperImpl(bean); // build a BeanWrapperImpl
        beanWrapper.setPropertyValue("a", "102");
        HashMap<String, String> map = new HashMap<>();
        map.put("b", "1999/08/1");
        map.put("c", "I am AirMan");
        beanWrapper.setPropertyValues(map);
        System.out.println(bean);

        // 3.DirectFieldAccessor ==> 为Bean的属性值赋值，必要时做类型转转换（走 Field）
        System.out.println("-----------------------DirectFieldAccessor-----------------------");
        TestDirectBeanImpl bean1 = new TestDirectBeanImpl();
        DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bean1);

        directFieldAccessor.setPropertyValue("a", "10");
        directFieldAccessor.setPropertyValue("b", "1999/08/1");
        directFieldAccessor.setPropertyValue("c", "I am AirMan");
        System.out.println(bean1);

        // 4.DataBinder ==> 为Bean的属性值赋值，必要时做类型转转换，根据 directFieldAccess决定走Property还是走Field
        System.out.println("-----------------------DataBinder-----------------------");
        TestDirectBeanImpl bean2 = new TestDirectBeanImpl();
        DataBinder dataBinder = new DataBinder(bean2);
        dataBinder.initDirectFieldAccess();
        MutablePropertyValues psv = new MutablePropertyValues();
        psv.add("a", "10");
        psv.add("b", "1999/03/04");
        psv.add("c", "hello");
        dataBinder.bind(psv);
        System.out.println(bean2);

    }


    static class TestBeanWrapperImpl {
        private int a;
        private Date b;
        private String c;

        public void setA(int a) {
            this.a = a;
        }

        public void setB(Date b) {
            this.b = b;
        }

        public void setC(String c) {
            this.c = c;
        }

        @Override
        public String toString() {
            return "TestBeanWrapperImpl{\n" +
                    "a=" + a +
                    ", \nb=" + b +
                    ", \nc='" + c + '\'' + "\n" +
                    '}';
        }
    }

    static class TestDirectBeanImpl {
        private int a;
        private Date b;
        private String c;

        @Override
        public String toString() {
            return "TestDirectBeanImpl{\n" +
                    "a=" + a +
                    ", \nb=" + b +
                    ", \nc='" + c + '\'' + "\n" +
                    '}';
        }
    }
}
